package com.shengyin.ec.controller.news;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.shengyin.ec.exception.ControllerException;
import com.shengyin.ec.model.entity.News;
import com.shengyin.ec.service.news.NewsService;

@Controller
public class NewsController {

	@Autowired
	NewsService newsService;
	
	@RequestMapping("/toNewsAdd.do")
	public String toNewsAdd() {

		return "redirect:/admin/news/newsAdd.jsp";
	}

	@RequestMapping("/toNewsModify.do")
	public String toNewsModify(@RequestParam("id") long id,Model model) {
		News news  =newsService.getNews(id);
		model.addAttribute("news", news);
		return "/admin/news/newsModify";
	}

	@RequestMapping("/toNewsList.do")
	public String toNewsList(Model model,HttpServletRequest request) {
		
		int offset = 0;
		try {
			offset = Integer.valueOf(request.getParameter("pager.offset"));
		} catch (Exception ex) {
			System.out.println("初始化页数.......");
		}
		ArrayList<News> newsList = (ArrayList<News>) newsService.getNewsList(offset, 10);//默认10条才进行分页
		Long maxSize=newsService.getTotal();
		model.addAttribute("maxSize", maxSize);
		model.addAttribute("newsList", newsList);
		return "/admin/news/newsList";
	}
	
	@RequestMapping("/newsAdd.do")
	public String createNews(NewsForm newsForm){
		News news= new News();
		try {
			BeanUtils.copyProperties(news, newsForm);
			String createTime =DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss");
			news.setCreateTime(createTime);
			news.setUpdateTime(createTime);
			newsService.createNews(news);
			return "redirect:/toNewsList.do";
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return null;
	}
	
	@RequestMapping("/newsDelete.do")
    public String deleteNews(@RequestParam("id") int id){
		newsService.removeNewsById(id);
		return "redirect:/toNewsList.do";
	}
	
	@RequestMapping("/deleteSelectedNews.do")
	public String deleteSelectedNews(HttpServletRequest request) throws ControllerException{
		String [] ids=  request.getParameterValues("ids");
		if(ids==null)
			throw new ControllerException("所选新闻不存在");
		newsService.deleteSelectedNews(ids);
		return "redirect:/toNewsList.do";
	}
	
    
    @RequestMapping("/newsModify.do")
    public String modifyNews(NewsForm newsForm){
    	News news=new News();
    	try {
			BeanUtils.copyProperties(news, newsForm);
			String updateTime =DateFormatUtils.format(new Date(), "yyyy.MM.dd HH:mm:ss");
			news.setUpdateTime(updateTime);
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	newsService.modifyNews(news);
    	return "redirect:/toNewsList.do";
    }
    
/*
 * 异常处理，可以让抛出到页面处理
 * */
	@ExceptionHandler({ControllerException.class})  
	   public String exception(ControllerException e,HttpServletRequest request) {  
	       request.setAttribute("error", e.getMessage());
	        return "/admin/error/error";  
	    }  
}
